package org.wang.gift.exception.asserts;

import org.wang.gift.exception.BusinessException;
import org.wang.gift.exception.code.StatusCode;

import java.util.List;

/**
 * 断言工具类
 */
public class Assert {
    /**
     * 空判断
     * @param obj
     */
    public static void assertNotNull(Object obj, StatusCode code){
        if (obj == null) {
            throw new BusinessException(code.getCode(), code.getMessage());
        }
    }

    /**
     * 判断字符串空串
     * @param str
     */
    public static void assertNotEmpty(String str,StatusCode code){
        if (str == null || str.trim().equals("")) {
            throw new BusinessException(code.getCode(), code.getMessage());
        }
    }
    /**
     * 判断List是空
     * @param list
     */
    public static void assertListIsEmpty(List<?> list, StatusCode code){
        if (list!=null && list.size()>0)
            throw new BusinessException(code.getCode(), code.getMessage());
    }

    /**
     * 判断List不为null
     * @param list
     */
    public static void assertListNotEmpty(List<?> list,StatusCode code){
        if (list==null || list.size()<1)
            throw new BusinessException(code.getCode(), code.getMessage());
    }

    /**
     * 判断相等
     * @param str1
     * @param str2
     */
    public static void assertEquals(String str1,String str2,StatusCode code){

        if (!str1.trim().equals(str2.trim()))
            throw new BusinessException(code.getCode(), code.getMessage());
    }
    /**
     * 判断不相等
     * @param str1
     * @param str2
     */
    public static void assertNotEquals(String str1,String str2,StatusCode code){

        if (str1.trim().equals(str2.trim()))
            throw new BusinessException(code.getCode(), code.getMessage());
    }
    /**
     * 判断不相等
     * @param str1
     * @param str2
     */
    public static void assertNotEqualsIgnoreCase(String str1,String str2,StatusCode code){

        if (str1.trim().equalsIgnoreCase(str2.trim()))
            throw new BusinessException(code.getCode(), code.getMessage());
    }

    public static void assertIsEmpty(String error, String errorDescription) {
        if (error != null)
            throw new BusinessException(StatusCode.CODE_400.getCode(),errorDescription);
    }
}
